import { login, logout, getUserInfo, typeListAll } from '@/api/api'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    info: {},
    typeList: [],
    typeListMap: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_USERS: (state, info) => {
    state.info = info
  },
  SET_TYPELIST: (state, typeList) => {
    state.typeList = typeList
    const map = new Map()
    typeList.forEach(item => {
      map[item.simplify] = item.title
    })
    state.typeListMap = map
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, captcha } = userInfo
    return new Promise((resolve, reject) => {
      // commit('SET_TOKEN', 'admin-token')
      // setToken('admin-token')
      // resolve()
      // return false

      return login({ userName: username.trim(), passWord: password, captcha: captcha }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      const data = {
        roles: ['admin'],
        introduction: 'I am a super administrator',
        avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
        name: 'Super Admin'
      }
      const { roles, name, avatar } = data
      // roles must be a non-empty array
      if (!roles || roles.length <= 0) {
        reject('getInfo: roles must be a non-null array!')
      }
      commit('SET_ROLES', roles)
      commit('SET_NAME', name)
      commit('SET_AVATAR', avatar)
      typeListAll().then(res => {
        commit('SET_TYPELIST', res.data)
        resolve(data)
      })

      return false
      getUserInfo().then(response => {
        const { data } = response
        if (!data) {
          return Promise.reject('Verification failed, please Login again.')
        }
        const { isSuper, name, phone } = data
        // // roles must be a non-empty array
        // if (!roles || roles.length <= 0) {
        //   reject('getInfo: roles must be a non-null array!')
        // }
        // if (isSuper) {
        //   commit('SET_ROLES', ['admin'])
        // }
        commit('SET_ROLES', ['admin'])

        commit('SET_NAME', name)
        commit('SET_USERS', data)
        commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
        data.roles = ['admin']
        typeListAll().then(res => {
          commit('SET_TYPELIST', res.data)
          resolve(data)
        })
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // removeToken() // must remove  token  first
      // resetRouter()
      // commit('RESET_STATE')
      // resolve()
      // return false

      logout().then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

